<template>
  <BasicTable
    ref="tableRef"
    :initial-params="initialParams"
    :query-api="wisdomErrors.queryAll"
    :query-fields="queryFields"
    :table-fields="tableFields"
    :has-add="false"
  ></BasicTable>
</template>

<script setup lang="tsx">
import { ElMessage } from "element-plus";
import { Fragment, ref } from "vue";
import { wisdomErrors } from "../../api/wisdomErrors";
import BasicTable from "../../components/BasicTable";
import parseTime from "../../utils/parseTime";

const initialParams = {
  limit: 99999,
};

const queryFields = [
  {
    prop: "name",
    title: "设备名称",
  },
  {
    prop: "bid",
    title: "设备id",
  },
  {
    prop: "type",
    title: "异常类型",
  },
];

const tableFields = [
  {
    prop: "bid",
    title: "设备id",
  },
  {
    prop: "name",
    title: "设备名称",
  },
  {
    prop: "type",
    title: "异常类型",
  },
  {
    prop: "gps",
    title: "坐标x",
  },
  {
    prop: "frequency",
    title: "异常次数",
  },
  {
    prop: "createTime",
    title: "异常时间",
    formatter: (row) => {
      return parseTime(row);
    },
  },
  {
    title: "操作",
    slot: (row) => (
      <Fragment>
        <el-button link type="primary" onClick={() => handleNavigation(row)}>
          一键导航
        </el-button>
      </Fragment>
    ),
  },
];

const loading = ref(false);
const tableRef = ref(null);

async function handleNavigation(row: any) {
  loading.value = true;
  try {
    const { message } = (await wisdomErrors.navigation({
      deviceId: row.bid,
      mid: row.mid,
    })) as any;
    ElMessage({ type: "success", message });
    tableRef.value.getList();
  } finally {
    loading.value = false;
  }
}
</script>
